﻿Imports Q7P1TTS.Lib
Public Class ServiceList
    Dim currentPage, maxPage, rowPerPage As Integer
    Dim dynamicSource As New List(Of Service)
    Private Sub ServiceList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim mainForm As MainForm = Me.FindForm()
        mainForm.AcceptButton = btnSearch
        rowPerPage = Convert.ToInt32(My.Settings.ROW_PER_PAGE)
        DataBind()

        btnAdd.Visible = mainForm.isAdmin
        btnEdit.Visible = mainForm.isAdmin
        btnDelete.Visible = mainForm.isAdmin
        btnDetail.Visible = mainForm.isCustomer
    End Sub

    Private Sub DataBind()
        Dim items As List(Of Service) = ServiceController.load().OrderByDescending(Function(s As Service) s.CreateDate).ToList()
        If Not String.IsNullOrEmpty(txtSearch.Text) Then
            Dim regEx As New System.Text.RegularExpressions.Regex(txtSearch.Text)
            items = items.Where(Function(i As Service) regEx.IsMatch(i.Title) Or regEx.IsMatch(i.Description)).Distinct().ToList()
        End If
        grdServiceList.AutoGenerateColumns = False
        grdServiceList.DataSource = items
        dynamicSource = items
        maxPage = Math.Ceiling(dynamicSource.Count / rowPerPage)
        cboPageSelector.Items.Clear()
        For i As Integer = 1 To maxPage Step 1
            cboPageSelector.Items.Add(i)
        Next
        If cboPageSelector.Items.Count > 0 Then
            cboPageSelector.SelectedIndex = 0
        End If
        Paging()
    End Sub

    Private Sub Paging()
        lblMaxPage.Text = "/" & maxPage.ToString()
        grdServiceList.DataSource = dynamicSource.Skip((currentPage - 1) * rowPerPage).Take(rowPerPage).ToList()

        btnFirst.Enabled = (currentPage > 1)
        btnPrev.Enabled = (currentPage > 1)
        btnNext.Enabled = (currentPage < maxPage)
        btnLast.Enabled = (currentPage < maxPage)
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        DataBind()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim main As MainForm = Me.FindForm()
        main.GoToForm(New CustomerForm())
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Dim main As MainForm = Me.FindForm()
        If grdServiceList.SelectedRows.Count <> 1 Then
            MessageBox.Show("Please select 1 record.")
        Else
            Dim obj As Service = grdServiceList.SelectedRows(0).DataBoundItem
            main.GoToForm(New ServiceForm(obj.ID), "Update Service: " & obj.Title.Substring(0, 25) & "...")
        End If
    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        cboPageSelector.SelectedIndex = 0
    End Sub

    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        cboPageSelector.SelectedIndex = cboPageSelector.SelectedIndex - 1
    End Sub

    Private Sub cboPageSelector_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPageSelector.SelectedIndexChanged
        currentPage = Convert.ToInt32(cboPageSelector.SelectedItem)
        Paging()
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        cboPageSelector.SelectedIndex = cboPageSelector.SelectedIndex + 1
    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        cboPageSelector.SelectedIndex = cboPageSelector.Items.Count - 1
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If grdServiceList.SelectedRows.Count <> 1 Then
            MessageBox.Show("Please select 1 record.")
        Else
            If MessageBox.Show("Are you sure", "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                Dim obj As Service = grdServiceList.SelectedRows(0).DataBoundItem
                ServiceController.delete(obj)
                DataBind()
            End If
        End If
    End Sub

    Private Sub grdServiceList_RowPrePaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles grdServiceList.RowPrePaint
        Try
            Dim com As Service = grdServiceList.Rows(e.RowIndex).DataBoundItem
            grdServiceList.Rows(e.RowIndex).Cells("CreateDate").Value = ConvertHelper.ToDateTime(com.CreateDate, "MM/dd/yyyy")
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnDetail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetail.Click
        If grdServiceList.SelectedRows.Count <> 1 Then
            MessageBox.Show("Please select 1 record.")
        Else
            Dim obj As Service = grdServiceList.SelectedRows(0).DataBoundItem
            Dim popup As New PopupHelper(New ServiceDetail(obj.ID), "Service Detail")
            popup.ShowDialog(Me.FindForm())
        End If
    End Sub
End Class
